//
//  ZYCTeaAttendanceStatisticsHeaderView.m
//  ZDSC
//
//  Created by 张威 on 2021/10/22.
//

#import "ZYCTeaAttendanceStatisticsHeaderView.h"
#import "UIButton+EnlargeEdge.h"
#import <FSCalendar.h>
@interface ZYCTeaAttendanceStatisticsHeaderView ()<FSCalendarDelegate,FSCalendarDataSource>

@property (nonatomic, strong) UIButton *previousButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) FSCalendar *calendar;
@property (nonatomic, strong) NSCalendar *gregorian;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@end
@implementation ZYCTeaAttendanceStatisticsHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"FFFFFF");
        [self addSubview:self.calendar];
        [self addSubview:self.previousButton];
        [self addSubview:self.nextButton];
        self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        

        [self.calendar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(46.0f);
        }];
        
        [self.previousButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.calendar).offset(95.0f);
            make.centerY.equalTo(self.calendar.mas_top).offset(27.0f);
            make.size.mas_equalTo(CGSizeMake(10, 8.5));
        }];

        [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.calendar).offset(-95.0f);
            make.centerY.equalTo(self.calendar.mas_top).offset(27.0f);
            make.size.mas_equalTo(CGSizeMake(10, 8.5));
        }];
        
        [self.previousButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
        [self.nextButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
    }
    return self;
}


#pragma mark - FSCalendarDelegate
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
    NSString *dateString = [self.dateFormatter stringFromDate:calendar.currentPage];    
    if ([self.delegate respondsToSelector:@selector(teaAttendanceStatisticsHeaderViewDidExchangedMonth:)]) {
        [self.delegate teaAttendanceStatisticsHeaderViewDidExchangedMonth:dateString];
    }
}

#pragma mark - getters
- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}

- (FSCalendar *)calendar {
    if (!_calendar) {
        _calendar = [[FSCalendar alloc] init];
        _calendar.delegate = self;
        _calendar.dataSource = self;
        _calendar.headerHeight = 50.0f;
        _calendar.locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
        _calendar.appearance.headerMinimumDissolvedAlpha = 0;
        _calendar.backgroundColor = [UIColor whiteColor];
        _calendar.placeholderType = FSCalendarPlaceholderTypeNone; //只显示当前月份
        _calendar.appearance.headerDateFormat = @"yyyy年MM月";// 设置头部时间格式
        _calendar.appearance.weekdayTextColor = HEX_RGB(@"#62676A");//设置周字体颜色
        _calendar.appearance.headerTitleColor = HEX_RGB(@"#262A28");//设置头字体颜色
        _calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
    }
    return _calendar;
}


- (UIButton *)previousButton {
    if (!_previousButton) {
        _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_previousButton setImage:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
        [_previousButton addTarget:self action:@selector(previousClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _previousButton;
}

- (UIButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setImage:[UIImage imageNamed:@"you"] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(nextClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}


- (void)previousClicked:(UIButton *)sender{
    
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *previousMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:previousMonth animated:YES];
}

- (void)nextClicked:(UIButton *)sender{
    NSDate *currentMonth = self.calendar.currentPage;
    NSDate *nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:currentMonth options:0];
    [self.calendar setCurrentPage:nextMonth animated:YES];
}
@end
